# encoding: utf-8 -*-
# Python3 
# Author：xiang
# Time：2023-05-06 15:45

import sys
import requests
import json
import os


def post_to_robot(result_info):
    # webhook：飞书群地址url
    webhook = result_info['push_url']
    # headers: 请求头
    headers = {'Content-Type': 'application/json'}

    # alert_headers: 告警消息标题
    alert_headers = result_info['casename']
    # alert_content: 告警消息内容，用户可根据自身业务内容，定义告警内容
    alert_content = f"{result_info['casename']}执行完成，请尽快处理异常！"
    JOB_URL = result_info['job_url']  # 附件文件名
    # message_body: 请求信息主体
    message_body = {
        "msg_type": "interactive",
        "card": {
            "config": {"wide_screen_mode": True},
            "elements": [{"tag": "div","text": {"content": alert_content,"tag": "lark_md"}},
                {
                    "actions": [{"tag": "button","text": {"content": "查看结果","tag": "lark_md"}, "url": JOB_URL,
                            "type": "default","value": {}}], "tag": "action"}],
            "header": {
                "template": "red",
                "title": {
                    "content": alert_headers,
                    "tag": "plain_text"
                }
            }
        }}
    requests.request("POST", webhook, headers=headers, data=json.dumps(message_body))

if __name__ == '__main__':
    result_info = {
        'plan_id': 5,
        'casename': '巅峰联赛稽核(端版)',
        'casecount': 5,
        'casesuc': 4,
        'casefail': 1,
        'caseskip': 0,
        'push_url': 'https://open.feishu.cn/open-apis/bot/v2/hook/117e8ae6-fa9a-4409-99fe-87c3b36d12ee',
        'job_url': 'http://192.168.1.57:8085/result_h5.html'
    }
    post_to_robot(result_info)